package com.ruoyi.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import javax.websocket.*;

@RestController
@Api(tags="身份证")
@RequestMapping(value = "/ray/card/")
public class ApiIdCardController {

    @ApiOperation("获取身份证信息")
    @GetMapping("get_id_card")
    public AjaxResult 获取身份证信息(){
        WebSocketClientEndpoint ws = new WebSocketClientEndpoint("ws://127.0.0.1:7415/");
        if(ws.error!=null){
            return AjaxResult.error(-1001,"刷卡设备异常:"+ws.error);
        }
        try {
            ws.sendMessage("04?timeout=10000");
            while (ws.msg==null){

            }
            String msg = ws.msg;
            ws.msg = null;
            ws.Close();
            String[] arr_str = StrUtil.split(msg, "||").toArray(new String[0]);
            String[] sfz_data = StrUtil.split(arr_str[0],"|").toArray(new String[0]);
//            System.out.printf(arr_str[1]);
            return AjaxResult.success(sfz_data[1],arr_str[0]);
        }catch (Exception e) {
            System.out.println("Error reconnecting: " + e.getMessage());
            return AjaxResult.error(-1002,"请放入身份证");
        }


    }





    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
            String message = "Hello, WebSocket Server!";
            session.getBasicRemote().sendText(message);
            System.out.println("Message sent: " + message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("Received message: " + message);
    }
}




